﻿@using SESM.DAL
@using SESM.DTO
@using SESM.Tools.Helpers
@{
    ViewBag.Title = "Server List";
    AccessLevel accessLevel = (AccessLevel) ViewData["AccessLevel"];
    List<EntityServer> serverList = ViewData["ServerList"] as List<EntityServer>;
    Dictionary<int, ServiceState> stateList = ViewData["StateList"] as Dictionary<int, ServiceState>;
}

@if (accessLevel == AccessLevel.Manager
    || accessLevel == AccessLevel.Admin
    || accessLevel == AccessLevel.SuperAdmin)
{
    <div class="btn-group btn-group-justified">
        @if(accessLevel == AccessLevel.SuperAdmin)
        {
            <a class="btn btn-default" href="@(Url.Action("Create", "Server"))">Add Server</a>
        }
        <a class="btn btn-success" href="@(Url.Action("StartAll", "Server"))">Start All</a>
        <a class="btn btn-warning" href="@(Url.Action("RestartAll", "Server"))">Restart All</a>
        <a class="btn btn-primary" href="@(Url.Action("StopAll", "Server"))">Stop All</a>
        <a class="btn btn-danger" onclick="return confirm('This will kill all running servers and may cause save corruption! Are you sure you want to do this?');" href="@(Url.Action("KillAll", "Server"))">Kill All</a>
    </div>
}
<table class="table table-striped table-hover">
    <tr>
        <th>
            Name
        </th>
        <th>
            State
        </th>

    </tr>

    @foreach (EntityServer item in serverList)
    {
        <tr onclick="location.href = '@(Url.Action("Status", "Server", new { id = item.Id }))'" class="hoverTable">
            <td>
                @item.Name
            </td>
            <td>
                @switch (stateList[item.Id])
                {
                    case ServiceState.Running:
                        <span class="glyphicon glyphicon-ok-sign green"></span>
                        break;
                    case ServiceState.Starting:
                    <span class="glyphicon glyphicon-chevron-up blue"></span>
                        break;
                    case ServiceState.Stopping:
                    <span class="glyphicon glyphicon-chevron-down blue"></span>
                        break;
                    case ServiceState.Stopped:
                    <span class="glyphicon glyphicon-remove-sign red"></span>
                        break;
                    case ServiceState.Unknow:
                    <span class="glyphicon glyphicon-warning-sign red"></span>
                        break;
                }
            </td>
        </tr>
    }

</table>
@if(serverList.Count == 0)
{
    <p>You don't have access to any servers.</p>
}

